﻿using CanDoo.Admin.Core.Contracts;
using CanDoo.Admin.Core.Models;
using CanDoo.Core.Context;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.FineUI;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;
using CanDoo.Secutiry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CanDoo.Oil.WebApp.cusCustomer
{
    public partial class Create1 : PageBase
    {

        public override string PageTitle
        {
            get
            {
                return "批量生成储值卡";
            }
        }
        public override string PageCode
        {
            get
            {
                return "cusCustomer";
            }
        }
        public override string ViewPower
        {
            get
            {
                return "cusCustomerview";
            }
        }

        private const string CONFIGPASSWORD = "%$@^HU^%W#G(%^G(";
        public ICustomerLevelContract CustomerLevelContract { get; set; }
        public IProductContract ProductContract { get; set; }
        public IUserContract UserContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }
        public IChinaAreaContract ChinaAreaContract { get; set; }
        public IOperator Operator { get; set; }
        public IDB DB { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIBindList.BindSex(ed_Sex, true);
                FineUIListHelper.BindDDL(CustomerLevelContract.EntityName, this.CustomerLevelContract.GetSelectList(), 0L, ed_CustomerLevel, true);

                FineUIListHelper.BindDDL("省份", ChinaAreaContract.GetSelectList(), 0L, ed_Province);
                var provinceCode = ed_Province.GetValue();
                FineUIListHelper.BindDDL("城市", ChinaAreaContract.GetSelectList(p => p.ParentCode == provinceCode), 0L, ed_City);
                var cityCode = ed_City.GetValue();
                FineUIListHelper.BindDDL("县区", ChinaAreaContract.GetSelectList(p => p.ParentCode == cityCode), 0L, ed_Area);
                var areaCode = ed_Area.GetValue();
                FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
            }
        }

        protected void ed_Province_SelectedIndexChanged(object sender, EventArgs e)
        {
            var provinceCode = ed_Province.GetValue();
            FineUIListHelper.BindDDL("城市", ChinaAreaContract.GetSelectList(p => p.ParentCode == provinceCode), 0L, ed_City);
            var cityCode = ed_City.GetValue();
            FineUIListHelper.BindDDL("县区", ChinaAreaContract.GetSelectList(p => p.ParentCode == cityCode), 0L, ed_Area);
            var areaCode = ed_Area.GetValue();
            FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
        }

        protected void ed_City_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cityCode = ed_City.GetValue();
            FineUIListHelper.BindDDL("县区", ChinaAreaContract.GetSelectList(p => p.ParentCode == cityCode), 0L, ed_Area);
            var areaCode = ed_Area.GetValue();
            FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
        }

        protected void ed_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            var areaCode = ed_Area.GetValue();
            FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var cardNoStartNo = ed_CardNoStart.GetValue();
            var cardNoEndNo = ed_CardNoEnd.GetValue();
            var Denomination = ed_Denomination.GetValue().ToDouble();
            var CustomerLevelId = ed_CustomerLevel.GetValue().ToLong();
            var remark = ed_Remark.GetValue();

            if (ed_CardNoEnd.GetValue().Length != ed_CardNoStart.GetValue().Length)
            {
                FineUIPro.Alert.ShowInTop("操作失败，起始卡号位数不相同");
                return;
            }

            var length = ed_CardNoStart.Text.Length;

            var cardNoStart = cardNoStartNo.ToLong();
            var cardNoEnd = cardNoEndNo.ToLong();
            if (cardNoEnd - cardNoStart > 1000)
            {
                FineUIPro.Alert.ShowInTop("一次只能做1000张卡");
                return;
            }

            if(CustomerContract.Count(p=> p.CardNo.CompareTo(cardNoStartNo) >= 0 && p.CardNo.CompareTo(cardNoEndNo) <= 0 ) > 0)
            {
                FineUIPro.Alert.ShowInTop("起始卡号间已经有卡号了。");
                return;
            }

            var result = DB.Save(() => {
                for (var i = cardNoStart; i <= cardNoEnd; i++)
                {
                    var cardNo = i.ToString().PadLeft(length, '0');

                    if(cardNo.Contains("4"))
                    {
                        continue;
                    }

                    var entity = new Customer();

                    entity.CardNo = cardNo;
                    entity.Name = "储值卡" + i.ToString();
                    entity.Balance = Denomination;
                    entity.Denomination = Denomination;
                    entity.Password = "888888";
                    entity.IsBurn = false;

                    entity.TotalAmount = 0.0;
                    entity.OnLinePayMoney = 0.0;
                    entity.CostedMoney = 0.0;
                    entity.CashBackAmount = 0.0;
                    entity.Integral = 0.0;
                    entity.IsOnline = false;
                    entity.IsLocked = false;
                    entity.StateUpdateTime = null;

                    entity.CardType = 1;
                    entity.CustomerLevelId = CustomerLevelId;
                    entity.Remark = remark;

                    entity.UserId = Operator.UserId.Value;

                    entity.User = new User();
                    entity.User.Id = entity.Id;
                    entity.User.Enable = true;
                    entity.User.UserType = 1;
                    entity.User.PasswordHash = PasswordHasher.HashPassword("888888");
                    entity.User.UserName = entity.CardNo;
                    entity.User.Name = entity.Name;

                    CustomerContract.AddEntity(entity);
                }
                return OperationResult.Success;
            });

            FineUIPro.Alert.ShowInTop(result.Message);
        }
    }
}